using System.Collections;
using System.Collections.Generic;
using UnityEngine;

internal class CommonAudioPlayer : IAudioPlayer
{
	private AudioManager m_AudioManager;

	private Dictionary<string, AudioClip> m_AudioPool;

	private AudioSource m_AudioSource;

	public CommonAudioPlayer(AudioManager audioManager)
	{
		m_AudioManager = audioManager;
		m_AudioPool = new Dictionary<string, AudioClip>();
		m_AudioSource = SupportTools.AddChild<AudioSource>(m_AudioManager.gameObject);
		m_AudioSource.playOnAwake = false;
		m_AudioSource.loop = true;
		m_AudioSource.name = "Loop source";
	}

	public IEnumerator Load(string audioFile)
	{
		WWW www = new WWW(StreamTools.GetStreamingAssetsPath(usePrefix: true) + "Audio/" + audioFile + ".wav");
		yield return www;
		if (!string.IsNullOrEmpty(www.error))
		{
			UnityEngine.Debug.LogError(www.error);
			yield break;
		}
		AudioClip clip = www.GetAudioClip(threeD: false, stream: true);
		m_AudioPool.Add(audioFile, clip);
	}

	public bool Unload(string audioFile)
	{
		if (!m_AudioPool.ContainsKey(audioFile))
		{
			return false;
		}
		m_AudioPool.Remove(audioFile);
		return true;
	}

	public void Resume(string audioFile)
	{
		if (!m_AudioSource.isPlaying)
		{
			m_AudioSource.Play();
		}
	}

	public void Pause(string audioFile)
	{
		if (m_AudioSource.isPlaying)
		{
			m_AudioSource.Pause();
		}
	}

	public void PauseAll()
	{
		Pause(string.Empty);
	}

	public void RemuseAll()
	{
		Resume(string.Empty);
	}

	public void Play(string audioFile, float volume)
	{
		if (m_AudioPool.ContainsKey(audioFile))
		{
			AudioClip audioClip = m_AudioPool[audioFile];
			if ((bool)audioClip)
			{
				m_AudioSource.PlayOneShot(audioClip, volume);
			}
		}
	}

	public void PlayLoop(string audioFile)
	{
		if (m_AudioPool.ContainsKey(audioFile))
		{
			AudioClip audioClip = m_AudioPool[audioFile];
			if ((bool)audioClip && m_AudioSource.clip != audioClip)
			{
				m_AudioSource.Stop();
				m_AudioSource.clip = audioClip;
				m_AudioSource.Play();
			}
		}
	}

	public void Stop(string audioFile)
	{
		if (m_AudioSource.clip != null)
		{
			m_AudioSource.Stop();
			m_AudioSource.clip = null;
		}
	}

	public void ReleasePool()
	{
		if ((bool)m_AudioSource)
		{
			UnityEngine.Object.Destroy(m_AudioSource.gameObject);
		}
		m_AudioPool = null;
	}
}
